#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <string>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include "glm_use.h"
#include "camera.h"
#include "Shader.h"
#include "Texture.h"
#include "model.h"
#include "constant.h"
#include "stb_image.h"
using namespace std;
using namespace glm;

typedef uint uint;

void processInput(GLFWwindow *);

float deltaTime = 0.0f;
float lastFrame = 0.0f;

Camera camera(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);

void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
	glViewport(0, 0, width, height);
}

bool firstMouse = true;
float lastX = DEFAULT_WINDOW_WIDTH / 2.0, lastY = DEFAULT_WINDOW_HEIGHT / 2.0;
void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
	if (firstMouse)
	{
		lastX = xpos;
		lastY = ypos;
		firstMouse = false;
	}
	float xoffset = xpos - lastX, yoffset = lastY - ypos;
	lastX = xpos, lastY = ypos;
	float sensitivity = 0.1;
	camera.mouseMove(sensitivity, xoffset, yoffset);
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
	camera.mouseScroll(static_cast<float>(xoffset), static_cast<float>(yoffset));
}
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
	if (key == GLFW_KEY_LEFT_ALT && action == GLFW_PRESS)
		glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
	if (key == GLFW_KEY_LEFT_ALT && action == GLFW_RELEASE)
		glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}

int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow *window = glfwCreateWindow(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT, "LearnOpenGL", NULL, NULL);
	if (window == NULL)
	{
		cout << "Failed to create GLFW window" << endl;
		glfwTerminate();
		exit(-1);
	}
	glfwMakeContextCurrent(window);
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		cout << "Failed to initialize GLAD" << endl;
		exit(-1);
	}
	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
	glfwSetCursorPosCallback(window, mouse_callback);
	glfwSetScrollCallback(window, scroll_callback);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	glfwSetKeyCallback(window, key_callback);

	Shader shader(".\\shaders\\vertexShader.glsl", ".\\shaders\\fragmentShader.glsl");

	ifstream model_file;
	model_file.open("mapping.txt");
	string model_name, texture_name;
	getline(model_file, model_name);
	getline(model_file, texture_name);
	Model mymodel(".\\model\\" + model_name, ".\\img\\" + texture_name);

	shader.use();
	glEnable(GL_DEPTH_TEST);

	while (!glfwWindowShouldClose(window))
	{
		processInput(window);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		shader.use();

		mat4 view = mat4(1.0f);
		view = camera.lookAt();

		mat4 projection = mat4(1.0f);
		projection = camera.projection();

		shader.setMat4("view", view);
		shader.setMat4("projection", projection);

		mat4 model = mat4(1.0f);
		model = translate(model, vec3(0.0f, 0.0f, 0.0f));
		model = scale(model, vec3(1.0f, 1.0f, 1.0f));
		shader.setMat4("model", model);

		mymodel.Draw(shader);

		float currentFrame = glfwGetTime();
		deltaTime = currentFrame - lastFrame;
		lastFrame = currentFrame;

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

void processInput(GLFWwindow *window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
	float cameraSpeed = 2.5f * deltaTime;
	camera.setSpeed(cameraSpeed);
	if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
		camera.moveForward();
	if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
		camera.moveBackward();
	if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
		camera.moveLeft();
	if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
		camera.moveRight();
	if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
		camera.moveUp();
	if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
		camera.moveDown();
	if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
		camera.reset();
}
